package when3meet.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import client.ServerAccess;

import when3meet.model.ClientInfo;
import when3meet.model.User;
import when3meet.view.AdminFrame;
import when3meet.view.Application;
import xml.Message;

/**
 * send remove/report request to server, and display corresponding report/affect
 * number of event to client
 * 
 * @author Xi
 * 
 */
public class RemoveController implements ActionListener {

	public void process(Message response) {

		Node update = response.contents.getFirstChild();
		NamedNodeMap map = update.getAttributes();
		String numberAffected = map.getNamedItem("numberAffected").getNodeValue();

		String str = AdminFrame.getInstance().getRemoveLabel().getText();
		String temp = numberAffected + " " + str;
		AdminFrame.getInstance().getRemoveLabel().setText(temp);
		AdminFrame.getInstance().getRemoveLabel().setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		sendRequest();
	}

	public void sendRequest() {

		AdminFrame frame = AdminFrame.getInstance();
		ClientInfo ci = ClientInfo.getInstance();
		String key = ci.getKey();
		boolean completed = frame.getRdbtnCompleted().isSelected();
		String input = frame.getInput().getText();
		String xmlString = null;

		if (input.contains("-")) {
			String id = input;
			xmlString = Message.requestHeader() + "<removeRequest " + "key = '" + key + "' " + "id = '" + id + "'/>"
					+ "</request>";
		} else {
			int daysOld = 0;
			daysOld = Integer.parseInt(input);
			xmlString = Message.requestHeader() + "<removeRequest " + "key = '" + key + "' " + "completed='"
					+ completed + "' " + "daysOld='" + daysOld + "'/>" + "</request>";
		}

		Message m = new Message(xmlString);

		ServerAccess access = Application.getInstance().getServerAccess();
		access.sendRequest(m);
	}
}
